define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hospital/hospital_discount/index' + location.search,
                    add_url: 'hospital/hospital_discount/add'+ location.search,
                    edit_url: 'hospital/hospital_discount/edit'+ location.search,
                    del_url: 'hospital/hospital_discount/del',
                    multi_url: 'hospital/hospital_discount/multi',
                    import_url: 'hospital/hospital_discount/import',
                    table: 'hospital_discount',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'hospital_id', title: __('Hospital_id'),operate:false,formatter: function (value,row,index) {
                            return row.hospital?.title;
                        }},
                        {field: 'goods_id', title: __('Goods_id'),operate:false,formatter: function (value,row,index) {
                            return row.goods?.title;
                        }},
                        {field: 'type', title: __('Type'), searchList: {"10":__('Type 10'),"20":__('Type 20')}, formatter: Table.api.formatter.normal},
                        {field: 'money', title: '折扣详细', operate:false,formatter: function (value,row,index) {
                            if(row.type == 10){
                                return '<span class="text-danger">'+row.discount+'%</span>';
                            }else{
                                return '<span class="text-danger">'+value+'元</span>';
                            }
                        }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("change", "#c-type", function(){
                //变更后的回调事件
                var type = $(this).val();
                if (type == 10) {
                    $("#c-money").parents('.form-group:first').addClass('hide');
                    $("#c-discount").parents('.form-group:first').removeClass('hide');
                } else {
                    $("#c-money").parents('.form-group:first').removeClass('hide');
                    $("#c-discount").parents('.form-group:first').addClass('hide');
                }
                $('#c-money').val('');
                $('#c-discount').val(100);
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("change", "#c-type", function(){
                //变更后的回调事件
                var type = $(this).val();
                if (type == 10) {
                    $("#c-money").parents('.form-group:first').addClass('hide');
                    $("#c-discount").parents('.form-group:first').removeClass('hide');
                } else {
                    $("#c-money").parents('.form-group:first').removeClass('hide');
                    $("#c-discount").parents('.form-group:first').addClass('hide');
                }
                $('#c-money').val('');
                $('#c-discount').val(100);
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
